package com.lzy.liujing.restaurant.controller.sysController;

import com.lzy.liujing.restaurant.Enums.ResultEnum;
import com.lzy.liujing.restaurant.entity.Result;
import com.lzy.liujing.restaurant.entity.ResultUrl;
import com.lzy.liujing.restaurant.utils.ImageUtil;
import com.lzy.liujing.restaurant.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/files")
public class FileUploadController {

    @Value("${web.upload-image-path}")
    private String location;

    @PostMapping("/upload")
    public ResponseEntity<?> uploadImage(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件为空");
        }

//        // 生成一个唯一的文件名
//        String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
//
//        // 将文件保存到指定目录
//        Path filePath = Paths.get(location, fileName);
//        Files.write(filePath, file.getBytes());

        // 返回图片的URL
        String imageUrl = "/" + file;
        return ResponseEntity.ok(imageUrl);
    }
}
